<?php
/**
 * 话题
 * ds
 * 2020.08.13
 */
namespace App\Models\MainDB;

use Illuminate\Database\Eloquent\Model;

class Theme extends Model{

    protected $table = 'ln_theme';

    protected $primaryKey = 'id';

    protected $fillable = [
        'id',
        'name',
        'start_time',
        'end_time',
        'chat_room_id',
        'is_deleted',
        'create_time',
        'update_time',
    ];

    public $timestamps = false;

    //获取列表数据，分页
    public function getListPaging($where = [], $fields = '*', $pagesize = 10)
    {
        return $this->select($fields)->where($where)->where('is_deleted', 0)->orderBy('id', 'desc')->paginate($pagesize);
    }

    public function getInfoById($id, $fields = ['id'])
    {
        $info = $this->where('id', $id)->where('is_deleted', 0)->select($fields)->first();
        return $info ? $info->toArray() : [];
    }

    public function add($data)
    {
        $data = array_merge($data, ['create_time' => time(), 'update_time' => time()]);
        return $this->insertGetId($data);
    }

    public function edit($id, $data)
    {
        if (!$this->getInfoById($id)) {
            return false;
        }
        $data = array_merge($data, ['update_time' => time()]);
        return $this->where('id', $id)->update($data);
    }

    public function del($id)
    {
        if (!$this->getInfoById($id)) {
            return false;
        }
        return $this->where('id', $id)->update(['is_deleted' => 1]);
    }
}